module common.hintmsg;

import std.datetime, std.string, std.stdio;

import scene.scene, mirgame, mirshare, mirsound;
import share.utility;

import allegro5.allegro;
import allegro5.allegro_font;
import allegro5.allegro_primitives;


public class HintMessage
{
    public string HintString;
    public string[] HintList;
    public int HintX, HintY, HintWidth, HintHeight;
    public bool HintUp;
    public ALLEGRO_COLOR HintColor;
    
    private Game _game;
    
    
    public this(Game game)
    {
        this._game = game;
        HintString = "";
        HintList = null;
        HintX = 0;
        HintY = 0;
        HintWidth = 0;
        HintHeight = 0;
        HintUp = false;
        HintColor = al_map_rgba(0, 0, 0, 0);
    }
    
    public void showHint(int x, int y, string str, ALLEGRO_COLOR color, bool drawup)
    {
        string data;
        int w, h;

        HintX = x;
        HintY = y;
        HintWidth = 0;
        HintHeight = 0;
        HintUp = drawup;
        HintColor = color;

        if (HintString != str)
        {
            clearHint();
            HintString = str;

            while (true)
            {
                if (str == "") break;
                str = GetValidStr3(str, data, ['\\']);
                w = MyPen.textWidth(data) + 4 /*¿©¹é*/ * 2;
                if (w > HintWidth) HintWidth = w;
                if (data != "")
                    HintList ~= data;
            }
            h = (MyPen.textHeight("A") + 1) * HintList.length + 3 /*¿©¹é*/ * 2;
            
            HintHeight = h;
            if (HintUp)
                HintY = HintY - HintHeight;
        }
    }
    
    public void clearHint()
    {
        HintList.length = 0;
    }
    
    public void render()
    {
        const HintBackColor = al_map_rgba(100, 149, 237, 200);
        const maxx = 200;
        const maxy = 300;
        
        int hx, hy;
        bool bodrawhintsurface = false;
        
        if (HintList.length > 0)
        {
            if (HintWidth > maxx) HintWidth = maxx;
            if (HintHeight > maxy) HintHeight = maxy;
            if (HintX + HintWidth > SCREENWIDTH) hx = SCREENWIDTH - HintWidth;
            else hx = HintX;
            if (HintY < 0) hy = 0;
            else hy = HintY;
            if (hx < 0) hx = 0;
            
            al_draw_filled_rectangle(hx, hy, hx + HintWidth, hy + HintHeight, HintBackColor);
            
            bodrawhintsurface = true;
        }
        
        if (bodrawhintsurface)
        {
            immutable int height = MyPen.textHeight("A");
            foreach (int i, string str; HintList)
                MyPen.drawText(hx + 4, hy + 3 + (height + 1) * i, HintColor, AlTransparentBlack, str);
        }
    }
}

